package flc.ast.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.CityJsonBean;
import flc.ast.databinding.ActivityMoreInfoBinding;
import flc.ast.dialog.EducationBgDialog;
import flc.ast.dialog.MaritalStatusDialog;
import flc.ast.dialog.PoliticalStatusDialog;
import flc.ast.utils.CityLevelsListDate;

public class MoreInfoActivity extends BaseAc<ActivityMoreInfoBinding> {

    private OptionsPickerView cityOptions, nativePlaceOptions;
    private CityLevelsListDate levelsListDate;
    private ArrayList<CityJsonBean> jsonBeans;
    private ArrayList<ArrayList<String>> arrayLists;
    private boolean hasMoreInfo;

    @Override
    protected int onCreate() {
        return R.layout.activity_more_info;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMoreInfoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.ivHasMoreInfo.setOnClickListener(this);
        mDataBinding.llEducationBackground.setOnClickListener(this);
        mDataBinding.llCity.setOnClickListener(this);
        mDataBinding.llMaritalStatus.setOnClickListener(this);
        mDataBinding.llPoliticalStatus.setOnClickListener(this);
        mDataBinding.llNativePlace.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                save();
                break;
            case R.id.ivHasMoreInfo://更多信息开关
                hasMoreInfo = !hasMoreInfo;
                mDataBinding.ivHasMoreInfo.setImageResource(hasMoreInfo ? R.drawable.agdxxk : R.drawable.agdxxg);
                break;
            case R.id.llEducationBackground://教育背景
                showEducationBgDialog();
                break;
            case R.id.llCity://所在城市
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    KeyboardUtils.hideSoftInput(this);
                }
                if (cityOptions != null) {
                    cityOptions.show();
                }
                break;
            case R.id.llMaritalStatus://婚姻状态
                showMaritalStatusDialog();
                break;
            case R.id.llPoliticalStatus://政治面貌
                showPoliticalStatusDialog();
                break;
            case R.id.llNativePlace://籍贯
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    KeyboardUtils.hideSoftInput(this);
                }
                if (nativePlaceOptions != null) {
                    nativePlaceOptions.show();
                }
                break;
        }
    }

    @Override
    protected void initData() {
//        hasMoreInfo = BasicInfoActivity.basicInfoBean.isHasMoreInfo();
//        mDataBinding.tvEducationBackground.setText(BasicInfoActivity.basicInfoBean.getEducationBackground());
//        mDataBinding.etHeight.setText(BasicInfoActivity.basicInfoBean.getHeight());
//        mDataBinding.etWeight.setText(BasicInfoActivity.basicInfoBean.getWeight());
//        mDataBinding.tvCity.setText(BasicInfoActivity.basicInfoBean.getCity());
//        mDataBinding.tvMaritalStatus.setText(BasicInfoActivity.basicInfoBean.getMaritalStatus());
//        mDataBinding.tvPoliticalStatus.setText(BasicInfoActivity.basicInfoBean.getPoliticalStatus());
//        mDataBinding.etNation.setText(BasicInfoActivity.basicInfoBean.getNation());
//        mDataBinding.tvNativePlace.setText(BasicInfoActivity.basicInfoBean.getNativePlace());
//        mDataBinding.etPersonalProfile.setText(BasicInfoActivity.basicInfoBean.getPersonalProfile());
//        mDataBinding.ivHasMoreInfo.setImageResource(hasMoreInfo ? R.drawable.agdxxk : R.drawable.agdxxg);
        levelsListDate = new CityLevelsListDate(mContext);
        jsonBeans = levelsListDate.initJsonData("citys_data.json");
        arrayLists = levelsListDate.initJsonData1("citys_data.json");
        showCityView();
        showNativePlaceView();
    }

    private void save() {
        String educationBackground = mDataBinding.tvEducationBackground.getText().toString().trim();
        if (TextUtils.isEmpty(educationBackground)) {
            ToastUtils.showShort(getString(R.string.please_sel_education_background));
            return;
        }
        String height = mDataBinding.etHeight.getText().toString().trim();
        if (TextUtils.isEmpty(height)) {
            ToastUtils.showShort(getString(R.string.please_input_height));
            return;
        }
        String weight = mDataBinding.etWeight.getText().toString().trim();
        if (TextUtils.isEmpty(weight)) {
            ToastUtils.showShort(getString(R.string.please_input_weight));
            return;
        }
        String city = mDataBinding.tvCity.getText().toString().trim();
        if (TextUtils.isEmpty(city)) {
            ToastUtils.showShort(getString(R.string.please_sel_city));
            return;
        }
        String maritalStatus = mDataBinding.tvMaritalStatus.getText().toString().trim();
        if (TextUtils.isEmpty(maritalStatus)) {
            ToastUtils.showShort(getString(R.string.please_sel_marital_status));
            return;
        }
        String politicalStatus = mDataBinding.tvPoliticalStatus.getText().toString().trim();
        if (TextUtils.isEmpty(politicalStatus)) {
            ToastUtils.showShort(getString(R.string.please_sel_political_status));
            return;
        }
        String nation = mDataBinding.etNation.getText().toString().trim();
        if (TextUtils.isEmpty(nation)) {
            ToastUtils.showShort(getString(R.string.please_input_nation));
            return;
        }
        String nativePlace = mDataBinding.tvNativePlace.getText().toString().trim();
        if (TextUtils.isEmpty(nativePlace)) {
            ToastUtils.showShort(getString(R.string.please_sel_native_place));
            return;
        }
        String personalProfile = mDataBinding.etPersonalProfile.getText().toString().trim();
        if (TextUtils.isEmpty(personalProfile)) {
            ToastUtils.showShort(getString(R.string.please_input_personal_profile));
            return;
        }
//        BasicInfoActivity.basicInfoBean.setHasMoreInfo(hasMoreInfo);
//        BasicInfoActivity.basicInfoBean.setEducationBackground(educationBackground);
//        BasicInfoActivity.basicInfoBean.setHeight(height);
//        BasicInfoActivity.basicInfoBean.setWeight(weight);
//        BasicInfoActivity.basicInfoBean.setCity(city);
//        BasicInfoActivity.basicInfoBean.setMaritalStatus(maritalStatus);
//        BasicInfoActivity.basicInfoBean.setPoliticalStatus(politicalStatus);
//        BasicInfoActivity.basicInfoBean.setNation(nation);
//        BasicInfoActivity.basicInfoBean.setNativePlace(nativePlace);
//        BasicInfoActivity.basicInfoBean.setPersonalProfile(personalProfile);
        ToastUtils.showLong(getString(R.string.more_info_save_suc));
        finish();
    }

    private void showEducationBgDialog() {
        EducationBgDialog educationBgDialog = new EducationBgDialog(mContext);
        educationBgDialog.setListener(new EducationBgDialog.Ilistener() {

            @Override
            public void clickConfirm(String education) {
                mDataBinding.tvEducationBackground.setText(education);
            }
        });
        educationBgDialog.show();
    }

    private void showMaritalStatusDialog() {
        MaritalStatusDialog maritalStatusDialog = new MaritalStatusDialog(mContext);
        maritalStatusDialog.setListener(new MaritalStatusDialog.Ilistener() {

            @Override
            public void clickConfirm(String maritalStatus) {
                mDataBinding.tvMaritalStatus.setText(maritalStatus);
            }
        });
        maritalStatusDialog.show();
    }

    private void showPoliticalStatusDialog() {
        PoliticalStatusDialog politicalStatusDialog = new PoliticalStatusDialog(mContext);
        politicalStatusDialog.setListener(new PoliticalStatusDialog.Ilistener() {

            @Override
            public void clickConfirm(String politicalStatus) {
                mDataBinding.tvPoliticalStatus.setText(politicalStatus);
            }
        });
        politicalStatusDialog.show();
    }

    /**
     * 初始化城市选择器
     */
    private void showCityView() {
        //条件选择器
        cityOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                try {
                    mDataBinding.tvCity.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                } catch (Exception e) {
                    mDataBinding.tvCity.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                }
            }
        }).setTitleText(getString(R.string.city_text))//标题
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleSize(14)//标题文字大小
                .setTitleColor(Color.parseColor("#000000"))//标题文字颜色
                .setSubmitColor(Color.parseColor("#2262D0"))//确定按钮文字颜色
                .setCancelColor(Color.parseColor("#A4A4A4"))//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setContentTextSize(16)//滚轮文字大小
                .build();
        cityOptions.setPicker(jsonBeans, arrayLists);
    }

    private void showNativePlaceView() {
        //条件选择器
        nativePlaceOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                try {
                    mDataBinding.tvNativePlace.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                } catch (Exception e) {
                    mDataBinding.tvNativePlace.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                }
            }
        }).setTitleText(getString(R.string.native_place))//标题
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleSize(14)//标题文字大小
                .setTitleColor(Color.parseColor("#000000"))//标题文字颜色
                .setSubmitColor(Color.parseColor("#2262D0"))//确定按钮文字颜色
                .setCancelColor(Color.parseColor("#A4A4A4"))//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setContentTextSize(16)//滚轮文字大小
                .build();
        nativePlaceOptions.setPicker(jsonBeans, arrayLists);
    }

}